var _extends = Object.assign || function (e) {
  for (var t = 1; t < arguments.length; t++) {
    var n, a = arguments[t];
    for (n in a) Object.prototype.hasOwnProperty.call(a, n) && (e[n] = a[n]);
  }
  return e;
}, _page_urls = require("../../utils/page_urls.js"), _page_urls2 = _interopRequireDefault(_page_urls), _tools = require("../../utils/tools");

function _interopRequireDefault(e) {
  return e && e.__esModule ? e : {
    default: e
  };
}

function _defineProperty(e, t, n) {
  return t in e ? Object.defineProperty(e, t, {
    value: n,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : e[t] = n, e;
}

var item, g, index, scrollTop, app = getApp(), page = app.page, request = app.util.request, route = app.route;

function add(e, t) {
  var n = parseInt(e[t]);
  "zan_num" === t ? n = 1 == e.is_zan ? n - 1 : n + 1 : n++, g.setData(_defineProperty({}, t, n)),
    index && (e = getCurrentPages()[getCurrentPages().length - 2], t = "news_list[" + index + "]." + t,
      e.route == _page_urls2.default.news && e.setData(_defineProperty({}, t, n)));
}

function getInfo(e) {
  var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : function () { };
  request({
    url: "ContractInfo",
    data: {
      contract_id: e,
      user_id: app.userInfo.id || wx.getStorageSync("userInfo").id
    },
    showLoading: !1,
    success: function (e) {
      wx.pageScrollTo({
        scrollTop: scrollTop
      }), g.setData(_extends({}, e)), t(e);
    }
  });
}


function wendaPay(order_id) {
  request({
    url: "ContractOrderPay",
    data: {
      order_id: order_id,
      user_id: app.userInfo.id || wx.getStorageSync("userInfo").id
    },
    success: function (t) {
      if (t._code == 1) {
        t.appId ? (0, _tools.wxPay)({
          data: t,
          success: function (t) {
            g.setData({
              isPayShow: false
            })
            wx.showToast({
              title: '您已成功支付，请点击右上角图标及时转存',
              icon: 'none'
            })
            dow(order_id)
          }
        }) : toast("错误:" + t);
      } else if (t._code == 2) {
        dow(order_id)
      }
    }
  });
}
function dow(order_id) {
  request({
    url: "ContractOrderFileUrl",
    data: {
      order_id: order_id,
      user_id: app.userInfo.id || wx.getStorageSync("userInfo").id
    },
    success: function (t) {
      if (t.code == 1) {
        wx.downloadFile({
          url: t.file_url,
          success: function (res) {
            wx.getFileSystemManager().saveFile({
              tempFilePath:res.tempFilePath,
              success:function(r){
                wx.openDocument({
                  filePath: r.savedFilePath,
                  showMenu:true
                })
              }
            })
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: t.msg,
          showCancel: false
        })
      }
    }
  });
}
page({
  data: {
    input_focus: !1,
    isLogin: !1,
    no_comment: app.srcRoot + "no_comment.png",
    isPayShow: false
  },
  onLoad: function (e) {
    g = this, e = (0, _tools.setQuery)(e), index = e.index || "", scrollTop = 0, getInfo(e.id, function (t) {
      request({
        url: "ContractViewAdd",
        data: {
          contract_id: t.id
        },
        showLoading: !1,
        success: function (e) {
          e ? add(t, "view_num") : wx.showToast({
            icon: "none",
            title: "错误:" + e
          });
        }
      });
    });
  },
  onShareAppMessage: function (e) {
    return {
      title: g.data.title,
      path: g.route + "?id=" + g.data.id + "&pid=" + app.userInfo.id,
      imageUrl: g.data.img || ""
    };
  },
  onShareTimeline: function () {
    return {
      title: g.data.title,
      query: "id=" + g.data.id + "&pid=" + app.userInfo.id,
      imageUrl: g.data.img || ""
    };
  },
  showComment: function (e) {
    (0, _tools.verifyLogin)({
      success: function () {
        request({
          url: "IsDownload",
          data: {
            contract_id: g.data.id,
            user_id: app.userInfo.id || wx.getStorageSync("userInfo").id
          },
          showLoading: !1,
          success: function (e) {
            if(e == 0){
              g.setData({
                isPayShow: true
              })
            } else {
              dow(g.data.id)
            }
          }
        });
        //wendaPay(g.data.id)
      }
    })
  },
  collect: function () {
    var t = 1 == (t = g.data.is_collect) ? 2 : 1;
    (0, _tools.verifyLogin)({
      success: function () {
        request({
          url: "ContractCollect",
          data: {
            type: t,
            contract_id: g.data.id,
            user_id: app.userInfo.id
          },
          showLoading: !1,
          success: function (e) {
            e ? g.setData({
              is_collect: t
            }) : wx.showToast({
              icon: "none",
              title: "错误:" + e
            });
          }
        });
      }
    });
  },
  zan: function () {
    var t = 1 == (t = g.data.is_zan) ? 2 : 1;
    (0, _tools.verifyLogin)({
      success: function () {
        request({
          url: "ContractZan",
          data: {
            type: t,
            contract_id: g.data.id,
            user_id: app.userInfo.id
          },
          showLoading: !1,
          success: function (e) {
            if (e) return g.setData({
              is_zan: t
            }), void add(g.data, "zan_num");
            wx.showToast({
              icon: "none",
              title: "错误:" + e
            });
          }
        });
      }
    });
  },

  onPageScroll: function (e) {
    scrollTop = e.scrollTop;
  },
  shareModal: function () {
    g.setData({
      isShow: !g.data.isShow
    });
  },
  payModal: function () {
    g.setData({
      isPayShow: !g.data.isPayShow
    });
  },
  poster: function () {
    g.shareModal(), route({
      url: "poster",
      params: {
        id: g.data.id,
        type: "newposter"
      }
    });
  },
  toHome: function () {
    route({
      url: "loading",
      type: "reLaunch"
    });
  },
  toVip: function () {
    g.setData({
      isPayShow: false
    })
    route({
      url: "/member/index/index"
    });
  },
  onPay: function(){
    wendaPay(g.data.id)
  }
});